package org.skydiveapp2;

import org.skydiveapp2.ui.InterfaceManager;
import org.skydiveapplibrary.repository.SkydiveTypeRepository;
import org.skydiveapplibrary.ui.SkydiveTypeSelectListAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class SkydiveTypeSelectListActivity extends BaseListActivity
{	
	@Override
    public void onCreate(Bundle savedInstanceState)
    { 
        super.onCreate(savedInstanceState);
        
        overridePendingTransition(R.anim.fadein,
				R.anim.hold);
        
        // init view
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		if (this.findViewById(R.id.tabletLayout) != null)
		{
			InterfaceManager.getInstance().configureTabletUI(this, 
					R.string.select_skydive_type_label);
		}
		else
		{
			InterfaceManager.getInstance().configurePhoneUI(this, 
					R.string.select_skydive_type_label);
		}
    }
    
    @Override
    public void onResume()
    {
        super.onResume();
        
        loadData();
    }
    
    @Override
    protected void onListItemClick(ListView listView, View view, int indx, long id)
    {
        // get current selection
        int previousSelectedId = getIntent().getIntExtra(Constants.EXTRA_SKYDIVE_TYPE_ID, -1);
        int newSelectedId = (int)id;
        
        // create new intent
        Intent intent =  new Intent();
        
        // if different than previous
        if (previousSelectedId != newSelectedId)
                intent.putExtra(Constants.EXTRA_SKYDIVE_TYPE_ID, newSelectedId);

        // set result, finish
                setResult(RESULT_OK, intent);
                close();
    }

    private void loadData()
    {
        // populate skydive types
        SkydiveTypeRepository repository = new SkydiveTypeRepository(this);
        SkydiveTypeSelectListAdapter adapter = new SkydiveTypeSelectListAdapter(
                                this,
                                repository.getSkydiveTypes());
                setListAdapter(adapter);
                
                // set initial selection
                int selectedId = getIntent().getIntExtra(Constants.EXTRA_SKYDIVE_TYPE_ID, -1);
                for (int index = 0; index < adapter.getCount(); index++)
                {
                        getListView().setItemChecked(index,
                                        adapter.getItems().get(index).getId() == selectedId); 
                }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_add, menu);
        inflater.inflate(R.menu.menu_back, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle item selection
        switch (item.getItemId())
        {
        case R.id.menu_add:
        	Intent intent = new Intent(this, SkydiveTypeEditActivity.class);
            startActivity(intent);
            return true;
        case R.id.menu_back:
        	close();
        	return true;
        
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
	@Override
    public void onBackPressed() {
		close();
    }
	
	private void close()
	{
		finish();
    	overridePendingTransition(R.anim.hold,
                R.anim.fadeout);
	}
} 